<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${serviceGuideType.id == null ? '添加' : '编辑'}分类</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/img_text.css">
    <link rel="stylesheet" href="${ctx}/static/uploadCI/upload.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <link rel="stylesheet" href="${ctx}/css/ui/mall/goods_form.css">
    <link rel="stylesheet" href="${ctx}/css/form-preview.css">
</head>
<body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/service/article/guidetype/save.do">
                    <input type="hidden" name="id" value="${serviceGuideType.id}"/>
                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称<span class="f-verify-red">*</span></label>
                            <div class="layui-input-block">
                                <input type="text" name="name" id="name" lay-verify="name" autocomplete="off"
                                       class="layui-input" value="${serviceGuideType.name}" maxlength="7"
                                       style="width: 200px; float: left; margin-right: 10px;"
                                >
                                <div class="layui-form-mid layui-word-aux">最多7字符</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">排序<span class="f-verify-red">*</span></label>
                            <div class="layui-input-block">
                                <input type="number" name="sort"
                                       class="layui-input" value="${serviceGuideType.sort}"
                                       style="width: 200px;  float: left; margin-right: 10px;" lay-verify="sort">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit
                                   lay-filter="*">立即提交</a>
                                <a href="${ctx}/service/article/guidetype/list.do"
                                   class="layui-btn layui-btn-primary">取消</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>
<script>
    txz.initHeader({
        nav: [{
            name: '指南分类',
            href: '${ctx}/service/article/guidetype/list.do'
        }, {
            name: '${serviceGuideType.id == null ? '添加' : '编辑'}分类',
            curr: true
        }],
        btns: [{
            type: 'back'
        }]
    });

    var form, laydate, formcache;
    $(function () {
        layui.use(['form', 'laydate'], function () {
            form = layui.form, laydate = layui.laydate;

            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.trim() == "") {
                        return '分类名不能为空';
                    } else {
                        var isRepeat =false;
                        $.ajax({
                            type: 'POST',
                            async: false, // 使用同步的方法
                            data: {
                                name: $("#name").val(),
                                id: '${serviceGuideType.id}'
                            },
                            dataType: 'json',
                            success: function (data) {
                                isRepeat = data.success;
                            },
                            url: '${ctx}/service/article/guidetype/verifyName.do'
                        });

                        if(!isRepeat){
                            return '分类名已存在';
                        }
                    }
                },
                sort: function (value) {
                    if (value.trim() == "") {
                        return "请填写序号";
                    }
                    if (value <= 0) {
                        return "请填写正确的序号";
                    }

                }
            });
            //监听提交
            form.on('submit', function (data) {
                submitForm(data);
            });

        });

    });


    function submitForm(data) {
        txz.submitObject(data.elem, function (callBack) {

            var formData = getFormData();
            var action = $("#myForm").attr("action");
            txz.ajaxRequest({
                method: 'post',
                url: action,
                saveCache: true,
                cacheObj: formcache,
                params: formData,
                callBack: function (res) {
                    typeof callBack === 'function' && callBack();
                    if (res.success) {
                        util.layerMsgSuccess("提交成功", function () {
                            location.href = "${ctx}/service/article/guidetype/list.do";
                        });
                    } else {
                        util.layerMsgError("提交失败")
                    }
                }
            });
        })
    }

    function getFormData() {
        var formData = util.serializeForm($('#myForm').serializeArray());
        return formData;
    }

</script>
</body>
</html>